package com.guigu.gulicommon.entity.pojo;


import lombok.*;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.List;

/**
 * 购物车
 *
 * @author 陈俊杰
 * @version 1.0
 * @since 2024/9/21 11:41
 */

@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
public class Cart {
    /**
     * 购物车项
     */
    @Getter
    @Setter
    private List<CartItem> cartItems;
    /**
     * 商品数量
     */
    private Long contNum;
    /**
     * 商品类型数量
     */
    private Long countType;
    /**
     * 总金额
     */
    private BigDecimal totalAmount;
    /**
     * 优惠金额
     */
    private BigDecimal reduce;

    /**
     * 获取商品总数
     *
     * @return 商品总数
     */
    public Long getContNum() {
        Long count = 0L;

        if (this.cartItems == null || this.cartItems.isEmpty()) {
            return count;
        }
        for (CartItem item : this.cartItems) {
            count += item.getCount();
        }
        return count;
    }

    /**
     * 获取总金额
     *
     * @return 总金额
     */
    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal("0");
        if (this.cartItems == null || this.cartItems.isEmpty()) {
            return amount;
        }
        //1 计算所有商品总价
        for (CartItem item : this.cartItems) {
            amount = amount.add(item.getTotalPrice());
        }
        //2 减去优惠
        if (this.reduce != null) {
            amount = amount.subtract(this.reduce);
            if (amount.compareTo(new BigDecimal("0")) < 0) {
                return new BigDecimal("0");
            } else {
                return amount;
            }
        }
        return amount;
    }

    /**
     * 获取商品类型数量
     *
     * @return
     */
    public Long getCountType() {
        long count = 0L;

        if (this.cartItems == null || this.cartItems.isEmpty()) {
            return count;
        }
        count = (long) this.cartItems.size();
        return count;
    }
}
